package zoo.gwt.client;

import java.util.List;

import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NodeList;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class GridEventHandler implements ClickHandler, MouseOverHandler,
		MouseOutHandler {

	private Element domElement;
	private Zoologico zoo;

	GridEventHandler(Element e, Zoologico zoologico) {
		this.domElement = e;
		this.zoo = zoologico;
	}

	@Override
	public void onClick(ClickEvent event) {

		if (zoo.isCriarHabitat()) {
			domElement.setClassName("animal-container-clicked-"
					+ this.zoo.getCorHabitat());
			domElement.setInnerHTML("<center><p>"
					+ this.zoo.getNomeHabitatAtual() + "</p></center>");
			setCellHabitat();

		} else if (zoo.isExcluirHabitat()) {
			if(!isDefaultStyle()) {	
				String className = domElement.getClassName();
				NodeList<Element> nodes = domElement.getParentElement()
						.getElementsByTagName("div");
				boolean confirm = Window
						.confirm("Tem certeza de que deseja excluir o Habitat selecionado?");
	
				if (confirm) {
					for (int i = 0; i < nodes.getLength(); i++) {
						if (nodes.getItem(i).getClassName().equals(className)) {
							nodes.getItem(i).setClassName("animal-container");
							nodes.getItem(i).setInnerHTML("");
						}
	
					}
					
				}
			}	
		}
		else if(!zoo.isAlgumBotaoClicado()) {
			if(getGridCell().getGridHabitat()!=null) {
				int x = event.getScreenX();
				int y = event.getClientY();
				showHabitatInfo(x, y);
				//Window.alert("testando o info de habitat!");
			}
		}

	}

	@Override
	public void onMouseOver(MouseOverEvent event) {

		if (isDefaultStyle() && zoo.isCriarHabitat()) {
			domElement.setClassName("animal-container-hover");
		}

	}

	@Override
	public void onMouseOut(MouseOutEvent event) {
		if (isDefaultHoverStyle())
			domElement.setClassName("animal-container");

	}
	
	public void showHabitatInfo(int x, int y) {

		final DialogBox dialogBox = new DialogBox();
		final Button closeButton = new Button("OK"); 
		final Label label = new Label();
		VerticalPanel panel = new VerticalPanel();
				
		dialogBox.setAutoHideEnabled(true);
		dialogBox.setGlassEnabled(true);
		dialogBox.addStyleName("popup");
		dialogBox.setPopupPosition(x, y);
		
		label.setText(getGridCell().getGridHabitat().getNome());
		label.addStyleName("label");
		
		closeButton.addStyleName("close-button");
		
		panel.add(label);
		panel.add(new HTML("<br />"));
		panel.add(getListagemAnimais());
		panel.add(new HTML("<br />"));
		panel.add(closeButton);
		
		/*dialogBox.setStyleName("popup");
		dialogBox.setWidth("200px");
		dialogBox.setHeight("150px") ;
		
		dialogBox.setHTML("<div class=\"popup-title\">"+getGridCell().getGridHabitat().getNome()+"</div>");
		

		final Button closeButton = new Button("Fechar");

		closeButton.getElement().setId("closeButton");
		VerticalPanel dialogVPanel = new VerticalPanel();
		dialogVPanel.setStyleName("dialogVPanel");
		dialogVPanel.setHeight("100%");
		//dialogVPanel.add(new HTML("<b>Animais:</b><br />"));
		
		dialogVPanel.add(getListagemAnimais());
		dialogVPanel.add(closeButton);
		dialogBox.setWidget(dialogVPanel);
*/		
		dialogBox.setWidget(panel);
		
		dialogBox.show();
		
		closeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				dialogBox.hide();
			}
		});
	}
	
	public boolean isDefaultStyle() {
		if(domElement.getClassName().equals("animal-container"))
			return true;
		return false;
	}
	
	public boolean isDefaultHoverStyle() {
		if(domElement.getClassName().equals("animal-container-hover"))
			return true;
		return false;
	}
	
	public int getRowIndex() {
		return Integer.parseInt(domElement.getId().substring(5, 6));
	}
	
	public int getColumIndex() {
		return Integer.parseInt(domElement.getId().substring(6, 7));
	}
	
	public void setCellHabitat() {
		Habitat habitat = new Habitat();
		habitat.setNome(zoo.getNomeHabitatAtual());
		
		if(!zoo.hasHabitat(zoo.getNomeHabitatAtual())) {
			zoo.addHabitat(habitat);
		}
		getGridCell().setGridHabitat(habitat);
	}
	
	public GridCell getGridCell() {
		return zoo.getZooGrid().getGridCell(getRowIndex(), getColumIndex());
	}
	
	public Widget getListagemAnimais() {
		List<Animal> animais = getGridCell().getGridHabitat().getAnimais();
		FlexTable animaisTable = new FlexTable();
		animaisTable.getRowFormatter().addStyleName(0, "tabela-header");
		animaisTable.setText(0, 0, "Nome");
		animaisTable.setText(0, 1, "Espécie");
		
		if(animais.size()>0) {
			for (Animal a : animais) {
				int row = animaisTable.getRowCount();
				animaisTable.setText(row, 0, a.getNome());
				animaisTable.setText(row, 1, a.getEspecie());
			}
			return animaisTable;
		}	
		else return new HTML("Não existem animais nesse Habitat!");
		
	}

}
